package com.huang.controller;

import cn.hutool.crypto.SecureUtil;
import com.huang.pojo.*;
import com.huang.service.AdminService;
import com.huang.service.StudentService;
import com.huang.service.TeacherService;
import com.huang.util.MailUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class TeacherController {

    @Autowired
    @Qualifier("AdminServiceImpl")
    private AdminService adminService;
    @Autowired
    @Qualifier("TeacherServiceImpl")
    private TeacherService teacherService;
    @Autowired
    @Qualifier("StudentServiceImpl")
    private StudentService studentService;

    //教师登陆
    @RequestMapping("/teacher/goLogin")
    public String goLogin() {
        return "teacherLogin";
    }

    @RequestMapping("/teacher/login")
    public String login(Teacher teacher, Model model, HttpSession session) {
        //密码加密
        teacher.setTeacherPwd(SecureUtil.md5(teacher.getTeacherPwd()));

        if (teacherService.loginTeacher(teacher) != null) {
            teacher=teacherService.findTeacherByNo(teacher);
            session.setAttribute("teacher", teacher);
            model.addAttribute("teacher", teacher);

            //加载头像
            UserAvatar userAvatar=new UserAvatar();
            userAvatar.setUserNo(teacher.getTeacherNo());
            model.addAttribute("userAvatar",studentService.JudgeAvatar(userAvatar));

            //***************************************************************************************************
            //添加老师活动跟踪日志
            UserDiary userDiary=new UserDiary();
            userDiary.setUserNo(teacher.getTeacherNo());
            Date now = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
            String time = dateFormat.format(now);
            userDiary.setUserLoginTime(time);
            if(studentService.stuLoginRecord(userDiary)!=0){
                System.out.println("登陆时间已成功记录"+time);
                session.setAttribute("teaLoginTime",time);
            }
            //***************************************************************************************************

            return "teacherIndex";
        } else {
            model.addAttribute("loginErrorMsg", "账号或密码有误");
            return "teacherLogin";
        }
    }

    //教师查看公告
    @RequestMapping("/teacher/toViewNotice")
    public String toViewNotice(Model model){
        //加载管理员发布的公告
        Announcement announcement = new Announcement();
        announcement.setNoticeStatus("T");
        List<Announcement> list = studentService.showNoticeStu(announcement);
        model.addAttribute("list", list);
        return "teacherViewNotice";
    }

    //教师完善信息
    @RequestMapping("/teacher/goImprove")
    public String goImprove(Teacher teacher, Model model) {
        model.addAttribute("teacher", teacherService.loginTeacher(teacher));

        UserAvatar userAvatar=new UserAvatar();
        userAvatar.setUserNo(teacher.getTeacherNo());
        model.addAttribute("userAvatar",studentService.JudgeAvatar(userAvatar));

        return "teacherImproveMsg";
    }

    @RequestMapping("/teacher/Improve")
    public String improve(Teacher teacher, Model model,HttpSession session) {
        if (teacherService.improveTeacherMsg(teacher) != 0) {
            model.addAttribute("rightImproveMsg", "个人信息更新成功");

            //***************************************************************************************************
            //添加教师活动跟踪日志
            UserDiary userDiary=new UserDiary();
            userDiary.setUserOperate("完善信息");

            Date nowRecord = new Date();
            SimpleDateFormat dateFormatRecord = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
            String timeRecord = dateFormatRecord.format(nowRecord);
            userDiary.setUserOperateTime(timeRecord);

            Teacher teacherRecord= (Teacher) session.getAttribute("teacher");
            userDiary.setUserNo(teacherRecord.getTeacherNo());

            String teaLoginTime= (String) session.getAttribute("teaLoginTime");
            userDiary.setUserLoginTime(teaLoginTime);
            if(studentService.stuOperateRecord(userDiary)!=0){
                System.out.println("完善信息已被成功记录"+timeRecord);
            }
            //***************************************************************************************************

            //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            //重新加载个人信息
            model.addAttribute("teacher", teacherService.loginTeacher(teacher));

            UserAvatar userAvatar=new UserAvatar();
            userAvatar.setUserNo(teacher.getTeacherNo());
            model.addAttribute("userAvatar",studentService.JudgeAvatar(userAvatar));

            //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

            return "teacherImproveMsg";
        } else {

            //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            //重新加载个人信息
            model.addAttribute("teacher", teacherService.loginTeacher(teacher));

            UserAvatar userAvatar=new UserAvatar();
            userAvatar.setUserNo(teacher.getTeacherNo());
            model.addAttribute("userAvatar",studentService.JudgeAvatar(userAvatar));

            //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

            model.addAttribute("errorImproveMsg", "个人信息更新失败");
            return "teacherImproveMsg";
        }
    }

    //教师发布活动
    @RequestMapping("/teacher/goRelease")
    public String goRelease(Teacher teacher, Model model) {
        model.addAttribute("teacher", teacherService.loginTeacher(teacher));
        return "teacherReleaseActivity";
    }

    @RequestMapping("/teacher/release")
    public String release(Activity activity, Model model, HttpSession session) {
        //初始化剩余名额
        activity.setActivityQuota(activity.getActivityPeopleNumber());

        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
        String time = dateFormat.format(now);
        activity.setActivityReleaseTime(time);

        activity.setActivityStatus("T");
        if (teacherService.releaseActivity(activity) != 0) {
            model.addAttribute("rightReleaseMsg", "活动发布成功");
            session.setAttribute("Msg", "活动发布成功");

            //***************************************************************************************************
            //添加教师活动跟踪日志
            UserDiary userDiary=new UserDiary();
            userDiary.setUserOperate("成功发布活动"+activity.getActivityNo());

            Date nowRecord = new Date();
            SimpleDateFormat dateFormatRecord = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
            String timeRecord = dateFormatRecord.format(nowRecord);
            userDiary.setUserOperateTime(timeRecord);

            Teacher teacherRecord= (Teacher) session.getAttribute("teacher");
            userDiary.setUserNo(teacherRecord.getTeacherNo());

            String teaLoginTime= (String) session.getAttribute("teaLoginTime");
            userDiary.setUserLoginTime(teaLoginTime);
            if(studentService.stuOperateRecord(userDiary)!=0){
                System.out.println("成功发布活动"+activity.getActivityNo()+"已被成功记录"+timeRecord);
            }
            //***************************************************************************************************

            return "redirect:/teacher/toQuery";
        } else {
            model.addAttribute("errorReleaseMsg", "活动发布失败");
            return "teacherReleaseActivity";
        }
    }

    //教师查询活动
    @RequestMapping("/teacher/toQuery")
    public String toQuery(Teacher teacher, Model model, HttpSession session) {
        if (teacher == null) {
            teacher = (Teacher) session.getAttribute("teacher");
        }
        model.addAttribute("Msg", (String) session.getAttribute("Msg"));
        model.addAttribute("teacher", teacherService.loginTeacher(teacher));
        model.addAttribute("list", teacherService.queryAllActivity(teacher));

        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
        String time = dateFormat.format(now);
        model.addAttribute("nowTime", time);

        return "teacherManageActivity";
    }

    //修改活动
    @RequestMapping("/teacher/toAlter")
    public String toAlter(Activity activity, Model model) {
        model.addAttribute("activity", teacherService.queryActivityByNo(activity.getActivityNo()));
        return "teacherAlterActivity";
    }

    @RequestMapping("/teacher/alter")
    public String alter(Activity activity, Model model, HttpSession session) {
        //联动活动剩余名额
        List<ActivityPeople> list=teacherService.queryActivityStudent2(activity);
        activity.setActivityQuota(activity.getActivityPeopleNumber()-list.size());

        if (teacherService.alterActivity(activity) != 0) {
            session.setAttribute("Msg", "修改成功");

            //***************************************************************************************************
            //添加教师活动跟踪日志
            UserDiary userDiary=new UserDiary();
            userDiary.setUserOperate("修改活动"+activity.getActivityNo());

            Date nowRecord = new Date();
            SimpleDateFormat dateFormatRecord = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
            String timeRecord = dateFormatRecord.format(nowRecord);
            userDiary.setUserOperateTime(timeRecord);

            Teacher teacherRecord= (Teacher) session.getAttribute("teacher");
            userDiary.setUserNo(teacherRecord.getTeacherNo());

            String teaLoginTime= (String) session.getAttribute("teaLoginTime");
            userDiary.setUserLoginTime(teaLoginTime);
            if(studentService.stuOperateRecord(userDiary)!=0){
                System.out.println("修改活动"+activity.getActivityNo()+"已被成功记录"+timeRecord);
            }
            //***************************************************************************************************

            return "redirect:/teacher/toQuery";
        } else {
            session.setAttribute("Msg", "修改失败");
            return "redirect:/teacher/toAlter";
        }
    }

    //删除活动
    @RequestMapping("/teacher/delete")
    public String delete(Activity activity, Model model, HttpSession session) {
        if (teacherService.deleteActivity(activity) != 0) {
            session.setAttribute("Msg", "删除成功");

            //***************************************************************************************************
            //添加教师活动跟踪日志
            UserDiary userDiary=new UserDiary();
            userDiary.setUserOperate("成功删除活动"+activity.getActivityNo());

            Date nowRecord = new Date();
            SimpleDateFormat dateFormatRecord = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
            String timeRecord = dateFormatRecord.format(nowRecord);
            userDiary.setUserOperateTime(timeRecord);

            Teacher teacherRecord= (Teacher) session.getAttribute("teacher");
            userDiary.setUserNo(teacherRecord.getTeacherNo());

            String teaLoginTime= (String) session.getAttribute("teaLoginTime");
            userDiary.setUserLoginTime(teaLoginTime);
            if(studentService.stuOperateRecord(userDiary)!=0){
                System.out.println("成功删除活动"+activity.getActivityNo()+"已被成功记录"+timeRecord);
            }
            //***************************************************************************************************

            return "redirect:/teacher/toQuery";
        } else {
            session.setAttribute("Msg", "删除失败");
            return "redirect:/teacher/toQuery";
        }

    }

    //查询参加活动的学生
    @RequestMapping("/teacher/toActivityStudent")
    public String toActivityStudent(Activity activity, Model model, HttpSession session) {

        List<ActivityPeople> list = teacherService.queryActivityStudent2(activity);
        //System.out.println(list);
        model.addAttribute("list", list);
        model.addAttribute("activity", activity);
        return "teacherManageActivityStudent";
    }

    //删除参加活动的某个学生
    @RequestMapping("/teacher/toRemove")
    public String toRemove(ActivityPeople activityPeople, Model model, HttpSession session) {

        System.out.println("删除参加活动的某个学生:" + activityPeople.getActivityNo());
        if (teacherService.removeActivityStudent(activityPeople) != 0) {
            session.setAttribute("rightRemoveMsg", "成功移除");

            //联动活动剩余名额
            Activity activity=teacherService.queryActivityByNo(activityPeople.getActivityNo());
            activity.setActivityQuota(activity.getActivityQuota()+1);
            if(studentService.updateActivityQuota(activity)!=0){
                System.out.println("成功踢出一个学生，剩余名额已加一");

                //***************************************************************************************************
                //添加教师活动跟踪日志
                UserDiary userDiary=new UserDiary();
                userDiary.setUserOperate("在活动"+activityPeople.getActivityNo()+"中踢出学生"+activityPeople.getStudentNo());

                Date nowRecord = new Date();
                SimpleDateFormat dateFormatRecord = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
                String timeRecord = dateFormatRecord.format(nowRecord);
                userDiary.setUserOperateTime(timeRecord);

                Teacher teacherRecord= (Teacher) session.getAttribute("teacher");
                userDiary.setUserNo(teacherRecord.getTeacherNo());

                String teaLoginTime= (String) session.getAttribute("teaLoginTime");
                userDiary.setUserLoginTime(teaLoginTime);
                if(studentService.stuOperateRecord(userDiary)!=0){
                    System.out.println("在活动"+activityPeople.getActivityNo()+"中踢出学生"+activityPeople.getStudentNo()+"已被成功记录"+timeRecord);
                }
                //***************************************************************************************************

            }

            //session.setAttribute("activityNoTmp",activityPeople.getActivityNo());
            return "redirect:/teacher/toActivityStudent";
            //return "test";
        } else {
            session.setAttribute("errorRemoveMsg", "移除失败");
            //session.setAttribute("activityNoTmp",activityPeople.getActivityNo());
            return "redirect:/teacher/toActivityStudent";
            //return "test";

        }
    }

    //老师查看自己发布过的活动
    @RequestMapping("/teacher/toViewTeaSelfActivity")
    public String toViewTeaSelfActivity(Teacher teacher, Model model, HttpSession session) {
        if (teacher.getTeacherNo() == null) {
            teacher = (Teacher) session.getAttribute("teacher");
        }
        model.addAttribute("Identity", "teacher");
        List<Activity> list = adminService.findActivityByTea(teacher);
        model.addAttribute("list", list);

        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
        String time = dateFormat.format(now);
        model.addAttribute("nowTime",time);
        return "adminViewActivity";
    }

    //老师改变活动状态
    @RequestMapping("/teacher/toChangeActivityStatus")
    public String toChangeActivityStatus(Activity activity, Model model,HttpSession session) {
        System.out.println("老师改变活动状态:" + activity);
        if (teacherService.changeActivityStatus(activity) != 0) {
            model.addAttribute("changeStatusRightMsg", "已成功变动");

            //***************************************************************************************************
            //添加教师活动跟踪日志
            UserDiary userDiary=new UserDiary();
            userDiary.setUserOperate("改变活动状态"+activity.getActivityNo());

            Date nowRecord = new Date();
            SimpleDateFormat dateFormatRecord = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
            String timeRecord = dateFormatRecord.format(nowRecord);
            userDiary.setUserOperateTime(timeRecord);

            Teacher teacherRecord= (Teacher) session.getAttribute("teacher");
            userDiary.setUserNo(teacherRecord.getTeacherNo());

            String teaLoginTime= (String) session.getAttribute("teaLoginTime");
            userDiary.setUserLoginTime(teaLoginTime);
            if(studentService.stuOperateRecord(userDiary)!=0){
                System.out.println("改变活动状态"+activity.getActivityNo()+"已被成功记录"+timeRecord);
            }
            //***************************************************************************************************

            return "redirect:/teacher/toViewTeaSelfActivity";
        } else {
            model.addAttribute("changeStatusErrorMsg", "未能成功变动");
            return "redirect:/teacher/toViewTeaSelfActivity";
        }
    }

    //老师激活自己的账号
    @RequestMapping("teacher/toActivation")
    public String toActivation(Teacher teacher, Model model) {
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
        String time = dateFormat.format(now);
        teacher.setTaecherActivation(time);

        teacher.setTeacherStatus("T");


        if (teacherService.changeTeacherStatus(teacher) != 0) {
            teacher = teacherService.findTeacherByCode(teacher);
            model.addAttribute("teacherActRightMsg", "账号已激活，" + teacher.getTeacherName() + "请牢记您的工号" + teacher.getTeacherNo() + "初始密码为您的工号");
            return "teacherActivationShow";
        } else {
            model.addAttribute("teacherActErrorMsg", "账号激活失败");
            return "teacherActivationShow";
        }

    }

    //老师查看历史公告
    @RequestMapping("/teacher/toViewHistoryNotice")
    public String toViewHistoryNotice(Model model) {
        Announcement announcement = new Announcement();
        announcement.setNoticeStatus("F");
        List<Announcement> list = studentService.showNoticeStu(announcement);
        model.addAttribute("list", list);
        model.addAttribute("Identity", "teacher");
        return "studentViewAllNotice";
    }

    //老师对公告进行反馈
    @RequestMapping("/teacher/toFeedBack")
    public String toFeedBack(FeedBack feedBack, Model model) {
        model.addAttribute("feedBack", feedBack);
        model.addAttribute("Identity", "teacher");
        return "studentFeedBack";
    }

    @RequestMapping("/teacher/feedBack")
    public String feedBack(FeedBack feedBack, Model model, HttpSession session) {
        Teacher teacher = (Teacher) session.getAttribute("teacher");
        feedBack.setFeedBackNo(teacher.getTeacherNo());
        feedBack.setFeedBackType("T");

        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
        String time = dateFormat.format(now);
        feedBack.setFeedBackTime(time);

        System.out.println("老师对公告进行反馈提交" + feedBack);
        if (teacherService.feedBackTea(feedBack) != 0) {
            model.addAttribute("feedBackRightMsg", "已反馈给管理员");

            //***************************************************************************************************
            //添加教师活动跟踪日志
            UserDiary userDiary=new UserDiary();
            userDiary.setUserOperate("对公告进行反馈"+feedBack.getAdminNotice());

            Date nowRecord = new Date();
            SimpleDateFormat dateFormatRecord = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
            String timeRecord = dateFormatRecord.format(nowRecord);
            userDiary.setUserOperateTime(timeRecord);

            Teacher teacherRecord= (Teacher) session.getAttribute("teacher");
            userDiary.setUserNo(teacherRecord.getTeacherNo());

            String teaLoginTime= (String) session.getAttribute("teaLoginTime");
            userDiary.setUserLoginTime(teaLoginTime);
            if(studentService.stuOperateRecord(userDiary)!=0){
                System.out.println("对公告进行反馈"+feedBack.getAdminNotice()+"已被成功记录"+timeRecord);
            }
            //***************************************************************************************************

            return "studentFeedBack";
        } else {
            model.addAttribute("feedBackErrorMsg", "出现错误，请重新提交");
            return "studentFeedBack";
        }
    }

    //老师查看自己的反馈
    @RequestMapping("/teacher/toShowSelfFeedBackTea")
    public String toShowSelfFeedBackTea(FeedBack feedBack, Model model, HttpSession session) {
        Teacher teacher = (Teacher) session.getAttribute("teacher");
        feedBack.setFeedBackNo(teacher.getTeacherNo());

        System.out.println("老师查看自己的反馈" + feedBack);
        List<FeedBack> list = studentService.showFeedBackStu(feedBack);
        model.addAttribute("list", list);
        return "studentShowSelfFeedBack";
    }

    //教师查看参加某个活动的学生
    @RequestMapping("/teacher/toViewActivityStu")
    public String toViewActivityStu(Activity activity, Model model) {
        model.addAttribute("Identity", "teacher");
        List<ActivityPeople> list = teacherService.queryActivityStudent2(activity);
        model.addAttribute("list", list);
        return "teacherManageActivityStudent";
    }

    //教师忘记密码
    @RequestMapping("/teacher/toForgetPwd")
    public String toForgetPwd() {
        return "teacherForgetPwd";
    }

    @RequestMapping("/teacher/forgetPwd")
    public String forgetPwd(Teacher teacher, Model model) {
        teacher = teacherService.findTeacherByNo(teacher);
        if (teacher != null) {
            //发送忘记密码邮件
            String content = "<a href='http://localhost:8080/teacher/toChangePwd?teacherCode=" + teacher.getTeacherCode() +
                    "&teacherName=" + teacher.getTeacherName() + "&teacherNo=" + teacher.getTeacherNo() + "'>重新设置您在学生素质拓展管理系统的登陆密码</a>" +
                    "<br>或者访问<br>http://localhost:8080/teacher/toChangePwd?teacherCode=" + teacher.getTeacherCode() +
                    "&teacherName=" + teacher.getTeacherName() + "&teacherNo=" + teacher.getTeacherNo();
            MailUtils.sendMail(teacher.getTeacherEmail(), content, "重新设置密码邮件");
            model.addAttribute("forgetRightMsg", "注意查收邮件进行重新设置密码");
            return "teacherForgetPwd";
        } else {
            model.addAttribute("forgetErrorMsg", "发送重新设置密码邮件失败，请联系管理员");
            return "teacherForgetPwd";
        }
    }

    @RequestMapping("/teacher/toChangePwd")
    public String toChangePwd(Teacher teacher, Model model) {
        model.addAttribute("teacher", teacher);
        return "teacherEmailChangePwd";
    }

    @RequestMapping("teacher/changePwd")
    public String changePwd(Teacher teacher, Model model) {
        //密码加密
        teacher.setTeacherPwd(SecureUtil.md5(teacher.getTeacherPwd()));
        if (teacherService.changePwdTea(teacher) != 0) {
            model.addAttribute("changeRightMsg", "密码更改成功");
            return "teacherEmailChangePwd";
        } else {
            model.addAttribute("changeErrorMsg", "密码更改失败，请联系管理员");
            return "teacherEmailChangePwd";
        }
    }

    //老师为活动增加活动详情
    @RequestMapping("/teacher/toAddActivityDetail")
    public String toAddActivityDetail(Activity activity,Model model,HttpSession session){
        if(activity.getActivityNo()!=null){
            session.setAttribute("activityData",activity);
        }else {
            activity= (Activity) session.getAttribute("activityData");
        }
        ActivityDescription activityDescription=teacherService.findActivityDescriptionByNo(activity);
        model.addAttribute("activityDescription",activityDescription);
        session.setAttribute("activityDescriptionData",activityDescription);
        model.addAttribute("activity",activity);

        return "teacherAddActivityDetail";
    }

    @RequestMapping("/teacher/addActivityDetail")
    public String addActivityDetail(ActivityDescription activityDescription,Model model,HttpSession session){
        Activity activity=new Activity();
        activity.setActivityNo(activityDescription.getActivityNo());
        activity.setActivityName(activityDescription.getActivityName());
        model.addAttribute("activity",activity);

        if(teacherService.addActivityDetail(activityDescription)!=0){
            model.addAttribute("activityDescription",activityDescription);
            model.addAttribute("activityDetailRightMsg","已成功提交");

            //***************************************************************************************************
            //添加教师活动跟踪日志
            UserDiary userDiary=new UserDiary();
            userDiary.setUserOperate("活动增加活动详情"+activityDescription.getActivityNo());

            Date nowRecord = new Date();
            SimpleDateFormat dateFormatRecord = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
            String timeRecord = dateFormatRecord.format(nowRecord);
            userDiary.setUserOperateTime(timeRecord);

            Teacher teacherRecord= (Teacher) session.getAttribute("teacher");
            userDiary.setUserNo(teacherRecord.getTeacherNo());

            String teaLoginTime= (String) session.getAttribute("teaLoginTime");
            userDiary.setUserLoginTime(teaLoginTime);
            if(studentService.stuOperateRecord(userDiary)!=0){
                System.out.println("活动增加活动详情"+activityDescription.getActivityNo()+"已被成功记录"+timeRecord);
            }
            //***************************************************************************************************

            return "redirect:/teacher/toAddActivityDetail";
        }else {
            model.addAttribute("activityDetailErrorMsg","未成功提交,请重试");
            return "redirect:/teacher/toAddActivityDetail";
        }
    }

    //老师查看活动详情
    @RequestMapping("/teacher/toViewActivityDescription")
    public String toViewActivityDescription(Activity activity,Model model) {
        ActivityDescription activityDescription=teacherService.findActivityDescriptionByNo(activity);
        model.addAttribute("activityDescription",activityDescription);
        return "adminViewActivityDetail";
    }

    //老师退出登陆
    @RequestMapping("/teacher/toLogout")
    public String toLogout(HttpSession session){

        Teacher teacherRecord= (Teacher) session.getAttribute("teacher");
        UserDiary userDiary=new UserDiary();
        userDiary.setUserNo(teacherRecord.getTeacherNo());
        String teaLoginTime= (String) session.getAttribute("teaLoginTime");
        userDiary.setUserLoginTime(teaLoginTime);
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
        String time = dateFormat.format(now);
        userDiary.setUserSignOutTime(time);
        if(studentService.stuLogoutRecord(userDiary)!=0){
            System.out.println("退出登录时间成功记录"+time);
        }

        session.removeAttribute("teacher");
        return "redirect:/teacher/goLogin";
    }

    //老师查看个人信息
    @RequestMapping("/teacher/toViewSelfInfo")
    public String toViewSelfInfo(Teacher teacher,Model model){
        teacher=teacherService.findTeacherByNo(teacher);
        model.addAttribute("teacher",teacher);
        return "teacherViewSelfInfo";
    }

    //老师生成微信明星片
    @RequestMapping("/teacher/toGenerateInfoCard")
    public String toGenerateInfoCard(Teacher teacher,Model model,HttpSession session){
        teacher=teacherService.findTeacherByNo(teacher);
        model.addAttribute("teacher",teacher);

        //***************************************************************************************************
        //添加教师活动跟踪日志
        UserDiary userDiary=new UserDiary();
        userDiary.setUserOperate("生成微信明星片");

        Date nowRecord = new Date();
        SimpleDateFormat dateFormatRecord = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
        String timeRecord = dateFormatRecord.format(nowRecord);
        userDiary.setUserOperateTime(timeRecord);

        Teacher teacherRecord= (Teacher) session.getAttribute("teacher");
        userDiary.setUserNo(teacherRecord.getTeacherNo());

        String teaLoginTime= (String) session.getAttribute("teaLoginTime");
        userDiary.setUserLoginTime(teaLoginTime);
        if(studentService.stuOperateRecord(userDiary)!=0){
            System.out.println("生成微信明星片已被成功记录"+timeRecord);
        }
        //***************************************************************************************************

        return "teacherGenerateInfoCard";
    }

    //教师查看日志
    @RequestMapping("/teacher/toViewDiary")
    public String toViewDiary(Teacher teacher,HttpSession session,Model model){
        List<UserDiary> list=teacherService.teaViewSelfDiary(teacher);
        model.addAttribute("list",list);

        return "studentOperationDiary";
    }

}
